﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace IKE.LCAFormat.ILCD
{
    public class AnyURIAttribute : Attribute
    {
    }

    public class DisplayUIAttribute : Attribute
    {
        public readonly CtrType Type;
        public readonly int Order;
        public Type ConverterType;
        public string GetNameText(Type parentType, string fieldName)
        {
            return ILCDFieldNameResMan.GetString(parentType.Name + "-" + fieldName);
        }
        public string GetDescText(Type parentType,string fieldName)
        {
            return ILCDFieldDescResMan.GetString(parentType.Name + "-" + fieldName);
        }
        public DisplayUIAttribute()
        { }
        public DisplayUIAttribute(int order)
            :this(CtrType.None,order)
        { }
        public DisplayUIAttribute(CtrType ctrlType, int order)
        {
            this.Type = ctrlType;
            this.Order = order;
        }

        public static List<FieldInfo> GetSortedMembers(FieldInfo[] fields)
        {
            Dictionary<int, FieldInfo> lst = new Dictionary<int, FieldInfo>();
            foreach (var item in fields)
            {
                var customAttributes = item.GetCustomAttributes(typeof(DisplayUIAttribute), false);
                if (customAttributes.Length > 0)
                {
                    var att = customAttributes[0] as DisplayUIAttribute;
                    lst.Add(att.Order, item);
                }
                else
                    throw new ArgumentException();
            }

            var query = from f in lst
                        orderby f.Key
                        select f.Value;
            return query.ToList();
        }

        public static string GetNameText(object parent, string memberName)
        {
            Type parentType = parent.GetType();
            var members = parentType.GetMember(memberName);
            var member = members[0];
            var atts = member.GetCustomAttributes(typeof(DisplayUIAttribute), false);
            if (atts.Length > 0)
            {
                var att = atts[0] as DisplayUIAttribute;
                return att.GetNameText(parentType, memberName);
            }
            return "";
        }

        public static string GetDescText(object parent, string memberName)
        {
            Type parentType = parent.GetType();
            var members = parentType.GetMember(memberName);
            var member = members[0];
            var atts = member.GetCustomAttributes(typeof(DisplayUIAttribute), false);
            if (atts.Length > 0)
            {
                var att = atts[0] as DisplayUIAttribute;
                return att.GetDescText(parentType,memberName);
            }
            return "";
        }
    }

    public class XmlnsAttribute : Attribute
    {
        public XmlnsAttribute(string name)
        {
            this.Name = name;
        }
        public string Name { get; set; }
    }

    public enum CtrType
    {
        None,
        NameType,
        MultiLang,
        TextInputS,
        TextInputM,
        CheckBox,
        DateTimePicker,
        ListGlobalRefType,
        ListGlobalRefType2,
        GlobalRefType,
        LocationCtr,
        ListLocationCtr,
        ListIntCtr,
        ListIntCtr2,
        ListComplianceTypeCtr,
        ValidationTypeCtr,
        ClassificationInfrTypeCtr,
        EnumValueDropDownList03,
        EnumValueDropDownList15,
        EnumValueDropDownList16,
        EnumValueDropDownList21,
        EnumValueDropDownList22,
        EnumValueDropDownList25,
    }

}
